package com.sys.service.impl;

import java.io.File;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.UUID;

import com.sys.constant.UploadStatus;
import com.sys.dao.impl.UploadDaoImpl;
import com.sys.domain.Upload;
import com.sys.service.UploadOperationSer;

public class UploadOperationImpl implements UploadOperationSer {

	@Override
	public String getFileName(String header) {
		String []aStr = header.split(";");
		String fileName = aStr[2].split("=")[1];
		fileName = fileName.replace("\"", "");
		return fileName;
	}

	@Override
	public Boolean addUpload(String fileId, String userId, Long size, String upTime)
			throws SQLException {
		Upload upload = new Upload();
		upload.setUserId(userId);
		upload.setFileId(fileId);
		upload.setUpTime(upTime);
		upload.setSize(size);
		upload.setStatus(UploadStatus.NOT_APPROVED);
		return new UploadDaoImpl().addUpload(upload);
	}

	@Override
	public String getUUID() {
		return UUID.randomUUID().toString();
	}

	@Override
	public String getCurrentDate() {
		SimpleDateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		return dFormat.format(System.currentTimeMillis());
	}

	@Override
	public String deleteUserIdOfFile(String str) {
		str = str.substring(9);
		return str;
	}

	@Override
	public Boolean isFileNameHasExisted(String fileName) throws SQLException {
		
		return new UploadDaoImpl().isFileHasUpload(fileName);
	}

	@Override
	public Long getFileSize(String path) {
		File file = new File(path);
		return file.length()/1024 + 1;
	}

	@Override
	public com.sys.domain.File setFileApproved(String fileId, String fileType) throws SQLException {
		com.sys.domain.File file = new com.sys.domain.File();
		Upload upload = new UploadDaoImpl().getAllInfoByFileId(fileId);
		file.setFileId(fileId);
		file.setFileType(fileType);
		file.setStatus("1");
		file.setSize(upload.getSize());
		file.setUpTime(upload.getUpTime());
		file.setUpUser(upload.getUserId());
		return file;
	}
}